//
// Created by Valentina_HP on 2023/3/16.
//
#include "rtthread.h"
#include "rthw.h"

#include "./common_inc.h"

#include "aurora_ui.h"
#include "AuroraCOMM.h"

#include "as5047_driver.h"
#include "mpu6050_driver.h"

#include "aurora_foc.h"

#include "LCD_thread.h"
#include "FOC_thread.h"
#include "COMM_thread.h"

#include "nrf24l01_driver.h"


uint16_t lcd_buff[320*80];

struct Aurora_UI2FOC_MSG __attribute__((section(".ccmram"))) ui2foc_msg; //用于UI发送FOC更新的邮箱
struct Aurora_FOC2UI_MSG __attribute__((section(".ccmram"))) foc2ui_msg; //用于FOC发送UI更新的邮箱

struct Aurora_COMM2UI_MSG __attribute__((section(".ccmram"))) comm2ui_msg;  //用于COMM发送UI更新的邮箱
struct Aurora_COMM2FOC_MSG __attribute__((section(".ccmram"))) comm2foc_msg;//用于COMM发送FOC更新的邮箱


struct Aurora_FOC2COMM_MSG __attribute__((section(".ccmram"))) foc2comm_msg; //用于FOC发送COM更新的邮箱

uint16_t __attribute__((section(".ccmram"))) hook_cnt = 0;
static void hook_of_scheduler(struct rt_thread *from,struct rt_thread *to);

void AppMain(void){

    HAL_GPIO_WritePin(FLASH_CS_GPIO_Port,FLASH_CS_Pin,GPIO_PIN_SET);
    HAL_GPIO_WritePin(NRF24L01L_CS_GPIO_Port,NRF24L01L_CS_Pin,GPIO_PIN_SET);
    HAL_GPIO_WritePin(NRF24L01L_CE_GPIO_Port,NRF24L01L_CE_Pin,GPIO_PIN_SET);
    HAL_GPIO_WritePin(SPI2_CS_GPIO_Port,SPI2_CS_Pin,GPIO_PIN_SET);
    HAL_GPIO_WritePin(SPI3_CS_GPIO_Port,SPI3_CS_Pin,GPIO_PIN_SET);

    rt_scheduler_sethook(hook_of_scheduler);
    //创建LCD 线程
    rt_thread_init(&lcd_thread,
                   "lcd_thread",
                   LCD_thread,
                   RT_NULL,
                   &lcd_thread_stack[0],
                   sizeof(lcd_thread_stack),
                   10, 5);
    rt_thread_startup(&lcd_thread);


    //创建FOC线程
    rt_thread_init(&foc_thread,
                   "foc_thread",
                   FOC_thread,
                   RT_NULL,
                   &foc_thread_stack[0],
                   sizeof(foc_thread_stack),
                   10, 5);
    rt_thread_startup(&foc_thread);

    //创建COMM(通信)线程
    rt_thread_init(&comm_thread,
                   "comm_thread",
                   COMM_thread,
                   RT_NULL,
                   &comm_thread_stack[0],
                   sizeof(comm_thread_stack),
                   10, 5);
    rt_thread_startup(&comm_thread);


//
//
 //  NRF24L01_Driver nrf24l01(&hspi2,NRF24L01L_CS_GPIO_Port,NRF24L01L_CS_Pin,NRF24L01L_CE_GPIO_Port,NRF24L01L_CE_Pin);
 //   Eflash_Driver eflash;      //外部flash驱动
 //   Aurora_Font font(&eflash);  //字体

  //  AS5047_Driver as5047L(&hspi3,SPI3_CS_GPIO_Port,SPI3_CS_Pin);
//    Eflash_Driver eflash;      //外部flash驱动
//    Aurora_Font font(&eflash);  //字体
//    uint32_t addr_offset = 0;
//    font.delete_font_data(Aurora_Font::font_size_middle);

//   NRF24L01_Driver nrf24l01(&hspi2,NRF24L01L_CS_GPIO_Port,NRF24L01L_CS_Pin,NRF24L01L_CE_GPIO_Port,NRF24L01L_CE_Pin);
//   MPU6050_Driver mpu6050(MPU6050_SCL_GPIO_Port,MPU6050_SCL_Pin,MPU6050_SDA_GPIO_Port,MPU6050_SDA_Pin);


    while( true ){

//      //  mpu6050.write(SMPLRT_DIV,100);
//
      //  int16_t data = (int16_t)(mpu6050.read(ACCEL_XOUT_L) + (int16_t)(mpu6050.read(ACCEL_XOUT_H) << 8));
     //   uint8_t ID= mpu6050.read_ID();//
       // usb_printf(" %d \r\n",eflash.read_jedec_id());
//        uint8_t data[5] = {0xA5,0x34,0x12,0xA5,0x65};
//        uint8_t d[5];
//        nrf24l01.write(NRF_WRITE_REG+NRF_TX_ADDR,data,5);
//        nrf24l01.read(NRF_TX_ADDR,d,5);
//        uint8_t  i;
//        for(i=0;i<5;i++)
//        {
//            usb_printf("nrf %x \r\n",d[i]);
//            HAL_Delay(10);
//        }
//        usb_printf(" %d\r\n",mpu6050.read_ID());
       rt_thread_mdelay(5);
//       // HAL_Delay(500);
    }
}



//SPI_DMATransmitCplt
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) {
//    if (flag == 0) {
//        flag = 1;
//    } else {
//        // LCD_CS_H;  //发送完成 拉高CS
//        flag = 0;
//    }
}


static void hook_of_scheduler(struct rt_thread *from,struct rt_thread *to){
    //usb_printf("test\r\n");
    hook_cnt += 1;
    if(hook_cnt < 500)
        return;
    hook_cnt = 0;
    if(HAL_GPIO_ReadPin(LED0_GPIO_Port,LED0_Pin) == GPIO_PIN_SET){
        HAL_GPIO_WritePin(LED0_GPIO_Port,LED0_Pin,GPIO_PIN_RESET);
        HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);
    }
    else{
        HAL_GPIO_WritePin(LED0_GPIO_Port,LED0_Pin,GPIO_PIN_SET);
        HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_RESET);
    }
}
